import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


@SuppressWarnings("serial")
public class EditContactDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfLastName;
	private JTextField tfFirstName;
	private JTextField tfStreet;
	private JTextField tfCity;
	private JTextField tfPostcode;
	private JTextField tfPrimaryPhone;
	private JTextField tfSecondaryPhone;
	private JTextField tfEmail;
	Contacts contacts;
	int index;
	String contactId;
	String[][] contact_table;
	/**
	 * Create the dialog.
	 */
	public EditContactDialog(Contacts con, int id) {
		contacts = con;
		index = id;
		contact_table = contacts.getContacts();
		setTitle("Bearbeiten Sie den Kontakt");
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screen.width/2)-180, (screen.height/2)-140 , 380, 300);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModal(true);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblLastName = new JLabel("Nachname*");
		lblLastName.setBounds(10, 11, 134, 14);
		contentPanel.add(lblLastName);
		
		tfLastName = new JTextField();
		tfLastName.setBounds(154, 8, 200, 20);
		contentPanel.add(tfLastName);
		tfLastName.setColumns(10);
		tfLastName.setText(contact_table[index][1]);
		
		JLabel lblFirstName = new JLabel("Vorname*");
		lblFirstName.setBounds(10, 36, 134, 14);
		contentPanel.add(lblFirstName);
		
		tfFirstName = new JTextField();
		tfFirstName.setBounds(154, 33, 200, 20);
		contentPanel.add(tfFirstName);
		tfFirstName.setColumns(10);
		tfFirstName.setText(contact_table[index][2]);
		
		JLabel lblStreet = new JLabel("Stra\u00DFe + Hausnummer*");
		lblStreet.setBounds(10, 61, 134, 14);
		contentPanel.add(lblStreet);
		
		tfStreet = new JTextField();
		tfStreet.setBounds(154, 58, 200, 20);
		contentPanel.add(tfStreet);
		tfStreet.setColumns(10);
		tfStreet.setText(contact_table[index][3]);
		
		JLabel lblCity = new JLabel("Ort*");
		lblCity.setBounds(10, 86, 134, 14);
		contentPanel.add(lblCity);
		
		tfCity = new JTextField();
		tfCity.setBounds(154, 83, 200, 20);
		contentPanel.add(tfCity);
		tfCity.setColumns(10);
		tfCity.setText(contact_table[index][4]);
		
		JLabel lblPostcode = new JLabel("Postleitzahl*");
		lblPostcode.setBounds(10, 111, 134, 14);
		contentPanel.add(lblPostcode);
		
		tfPostcode = new JTextField();
		tfPostcode.setBounds(154, 108, 200, 20);
		contentPanel.add(tfPostcode);
		tfPostcode.setColumns(10);
		tfPostcode.setText(contact_table[index][5]);
		
		JLabel lblPrimaryPhone = new JLabel("1.Kontaktnummer*");
		lblPrimaryPhone.setBounds(10, 136, 134, 14);
		contentPanel.add(lblPrimaryPhone);
		
		tfPrimaryPhone = new JTextField();
		tfPrimaryPhone.setBounds(154, 133, 200, 20);
		contentPanel.add(tfPrimaryPhone);
		tfPrimaryPhone.setColumns(10);
		tfPrimaryPhone.setText(contact_table[index][6]);
		
		JLabel lblSecondaryPhone = new JLabel("2.Kontaktnummer");
		lblSecondaryPhone.setBounds(10, 161, 134, 14);
		contentPanel.add(lblSecondaryPhone);
		
		tfSecondaryPhone = new JTextField();
		tfSecondaryPhone.setBounds(154, 158, 200, 20);
		contentPanel.add(tfSecondaryPhone);
		tfSecondaryPhone.setColumns(10);
		tfSecondaryPhone.setText(contact_table[index][7]);
		
		JLabel lblEmail = new JLabel("eMail");
		lblEmail.setBounds(10, 186, 134, 14);
		contentPanel.add(lblEmail);
		
		tfEmail = new JTextField();
		tfEmail.setBounds(154, 183, 200, 20);
		contentPanel.add(tfEmail);
		tfEmail.setColumns(10);
		tfEmail.setText(contact_table[index][8]);
		
		JLabel lblPflichtfeld = new JLabel("(*) Pflichtfeld");
		lblPflichtfeld.setBounds(10, 211, 134, 14);
		contentPanel.add(lblPflichtfeld);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton saveButton = new JButton("Speichern");
				saveButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					 if(	tfLastName.getText() != "" 		&& 
							tfFirstName.getText() != "" 	&& 
							tfStreet.getText() != "" 		&&
							tfCity.getText() != ""			&&
							tfPostcode.getText() != ""		&&
							tfPrimaryPhone.getText() != ""){
						String[] contactEdit = new String[9];
						contactEdit[0] = contact_table[index][0];
						contactEdit[1] = tfLastName.getText(); 
						contactEdit[2] = tfFirstName.getText();
						contactEdit[3] = tfStreet.getText();
						contactEdit[4] = tfCity.getText();
						contactEdit[5] = tfPostcode.getText();
						contactEdit[6] = tfPrimaryPhone.getText();
						contactEdit[7] = tfSecondaryPhone.getText();
						contactEdit[8] = tfEmail.getText();
						contacts.editContact(contactEdit,index);
						contacts.updateContactTable();
						setVisible(false);
						dispose();
					 }else{
						System.out.println("Nope");
					 }
					}
				});
				saveButton.setActionCommand("OK");
				buttonPane.add(saveButton);
				getRootPane().setDefaultButton(saveButton);
			}
			{
				JButton discardButton = new JButton("Verwerfen");
				discardButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
						dispose();
					}
				});
				discardButton.setActionCommand("Cancel");
				buttonPane.add(discardButton);
			}
		}
	}
}
